package com.sll.hosptials.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sll.hosptials.controller.req.AddDictTypeReq;
import com.sll.hosptials.controller.req.DictTypePageReq;
import com.sll.hosptials.controller.req.IdReq;
import com.sll.hosptials.controller.req.IdsReq;
import com.sll.hosptials.model.DictInfo;
import com.sll.hosptials.model.DictType;
import com.sll.hosptials.service.DictTypeService;
import com.sll.hosptials.mapper.DictTypeMapper;
import com.sll.hosptials.vo.ServiceException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

import static com.sll.hosptials.config.ParamConfig.ENABLE;

/**
 * @author shaolulu
 * @description 针对表【dict_type(字典类型)】的数据库操作Service实现
 * @createDate 2024-01-04 15:47:03
 */
@Service
public class DictTypeServiceImpl extends ServiceImpl<DictTypeMapper, DictType> implements DictTypeService {
    private Integer batchSize = 100;

    @Override
    public Boolean addDictType(AddDictTypeReq req) {
        LambdaQueryWrapper<DictType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DictType::getDictTypeCode, req.getDictTypeCode());
        if (Optional.ofNullable(this.getOne(queryWrapper)).isPresent()) {
            throw new ServiceException("字典类型CODE不能重复");
        }
        return this.save(BeanUtil.copyProperties(req, DictType.class));
    }

    @Override
    public DictType detail(IdReq req) {
        DictType dictType = getDictType(req.getId());
        return dictType;

    }

    @Override
    public Boolean delete(IdsReq req) {
//        如果字典类型已被使用，则不能删除
        LambdaQueryWrapper<DictInfo> queryWrapper=new LambdaQueryWrapper<>();
//        queryWrapper.eq(DictInfo::getDictTypeCode)

        if (CollectionUtil.isNotEmpty(req.getIds())) {
            this.removeBatchByIds(req.getIds(), batchSize);
        }
        return Boolean.TRUE;
    }

    @Override
    public Page<DictType> page(DictTypePageReq req) {
        LambdaQueryWrapper<DictType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNoneBlank(req.getDictTypeName()), DictType::getDictTypeName, req.getDictTypeName())
                .like(StringUtils.isNoneBlank(req.getDictTypeCode()), DictType::getDictTypeCode, req.getDictTypeCode())
                .orderByDesc(DictType::getDictTypeSort, DictType::getCreateDateTime);
        Page<DictType> page = this.page(new Page<>(req.getPageNo(), req.getPageSize()), queryWrapper);
        return page;
    }

    @Override
    public List<DictType> list() {
        LambdaQueryWrapper<DictType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(DictType::getDictTypeSort, DictType::getCreateDateTime);
        return this.list(queryWrapper);
    }


    private DictType getDictType(Long id) {
        return Optional.ofNullable(this.getById(id))
                .orElseThrow(() -> new ServiceException("不存在id为" + id + "的字典类型"));
    }

}




